package com.terren.spider.util.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import com.terren.spider.util.BasicUtil;

public class HttpUtil extends BasicUtil {
	/****************************
	 * set userAgent begin
	 **********************************************************/
	private static ArrayList<String> uaList;
	private static boolean loadUa = false;
	private static final String WEB_UA_FILE = CommonUtil.getProjectPath(HttpUtil.class) + "/data/web_ua.txt";
	private static final String MOBILE_UA_FILE = CommonUtil.getProjectPath(HttpUtil.class) + "/data/mobile_ua.txt";
	
	public static String getRandomWebUa() {
		if (!loadUa)
			uaList = importUserAgent(WEB_UA_FILE);
		int n = (int) Math.floor(Math.random() * uaList.size());
		int i = 0;
		String randomUa = "";
		for (String ua : uaList) {
			if (i == n) {
				randomUa = ua;
				break;
			}
			i++;
		}
		return randomUa;
	}

	public static String getRandomMobileUa() {
		if (!loadUa)
			uaList = importUserAgent(MOBILE_UA_FILE);
		int n = (int) Math.floor(Math.random() * uaList.size());
		int i = 0;
		String randomUa = "";
		for (String ua : uaList) {
			if (i == n) {
				randomUa = ua;
				break;
			}
			i++;
		}
		return randomUa;
	}

	private static ArrayList<String> importUserAgent(String filePath) {
		ArrayList<String> uaLs = new ArrayList<String>();
		try {
			File file = new File(filePath);
			InputStreamReader fr = new InputStreamReader(new FileInputStream(file), "GBK");
			BufferedReader br = new BufferedReader(fr);
			String str = br.readLine();
			while (str != null && str.length() > 0) {
				uaLs.add(str);
				str = br.readLine();
			}
			br.close();
			fr.close();
			loadUa = true;
			LogUtil.logger.info("Reading " + uaLs.size() + " UserAgent.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return uaLs;
	}

	/****************************
	 * set userAgent end
	 **********************************************************/

	/****************************
	 * set cookie begin
	 **********************************************************/
	private static final String WEB_COOKIE_FILE = CommonUtil.getProjectPath(HttpUtil.class) + "/data/wbcookie.txt";

	private static ArrayList<String> cookieList = new ArrayList<String>();
	private static boolean loadCookie = false;

	public static String getRandomWbCookie() {
		if (!loadCookie)
			cookieList = importWbCookie();
		int n = (int) Math.floor(Math.random() * cookieList.size());
		int i = 0;
		String randomCookie = "";
		for (String cookie : cookieList) {
			if (i == n) {
				randomCookie = cookie;
				break;
			}
			i++;
		}
		LogUtil.logger.info("Fetch a cookie that " + getUN(randomCookie));
		return randomCookie;
	}

	private static String getUN(String cookie) {
		return cookie.substring(cookie.indexOf("un="));
	}

	private static ArrayList<String> importWbCookie() {
		ArrayList<String> cookieLs = new ArrayList<String>();
		try {
			File file = new File(WEB_COOKIE_FILE);
			InputStreamReader fr = new InputStreamReader(new FileInputStream(file), "GBK");
			BufferedReader br = new BufferedReader(fr);
			String str = br.readLine();
			while (str != null && str.length() > 0) {
				cookieLs.add(str);
				str = br.readLine();
			}
			br.close();
			fr.close();
			loadCookie = true;
			LogUtil.logger.info("Reading " + cookieLs.size() + " Weibo cookies.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cookieLs;
	}

	/****************************
	 * set cookie end
	 **********************************************************/

	/****************************
	 * set request begin
	 **********************************************************/
/*
	public static Request initHttpProxyRequest(Entry entry,String url) {
		String host = entry.getProxyHost();
		Integer port = entry.getProxyPort();
		DynamicType dynamicType = DynamicType.valueOfType(entry.getDynamic());
		String server = "http://"+host+":"+port+"/?urlid="+Lookup3Hash.toID(url);//加urlid是为了去掉重复提交的url
		Request request = initRequest(server, url, dynamicType);
		
		
		return request;
	}
	
	
	public static Request initRequest(String targetUrl,String paramUrl,DynamicType dynamicType) {
		Request request = new Request(targetUrl);
		Map<String, Object> params = new HashMap<>();
		params.put("url", paramUrl);
		switch (dynamicType) {
		case DYNAMIC_DISENABLE:
			params.put("dynamic", false);
			break;
		case DYNAMIC_ENABLE:
			params.put("dynamic", true);
			break;

		default:
			params.put("dynamic", false);
			break;
		}
		request.setExtras(params);// 存放参数，可以从page.getRequest()中取到
		request.setRequestBody(HttpRequestBody.form(params, "utf-8"));
		request.setMethod(HttpConstant.Method.POST);
		return request;
	}
	*//**
	 * 初始化 request
	 * @param entry
	 * @param urls
	 * @return
	 *//*
	public static Request[] initRequests(Entry entry,String... urls) {
		Request[] requests = null;
		if (null != urls) {
			requests = new Request[urls.length];
			for (int index = 0; index < urls.length; index++) {
				requests[index] = initRequest(urls[index],urls[index],DynamicType.valueOfType(entry.getDynamic()));
			}
		}
		return requests;
	}
	
	*//**
	 * 初始化 requests（代理）
	 * @param entry
	 * @param urls
	 * @return
	 *//*
	
	
	public static Request[] initHttpProxyRequests(Entry entry,String... urls) {
		Request[] requests = null;
		
		if (null != urls) {
			requests = new Request[urls.length];
			for (int index = 0; index < urls.length; index++) {
				requests[index] = initHttpProxyRequest(entry,urls[index]);
			}
		}
		return requests;
	}
	
	
	public static Request[] getRequests(Boolean useProxy,Entry entry, String... urls) {
		*//**
		 * 数值去掉空值和空串
		 *//*
		urls = Arrays.stream(urls)
                .filter(s -> (s != null && s.length() > 0))
                .toArray(String[]::new);
		
		Request[] requests = null;
		if (useProxy) {// 使用代理
			requests = HttpUtil.initHttpProxyRequests(entry, urls);
		} else {// 不使用代理
			requests = HttpUtil.initRequests(entry, urls);
		}
		return requests;
	}
	
	*/
	
	
}
